<?php

namespace app\award;

use app\models\order\Order;
use app\models\order\OrderItem;
use app\models\SetUserUp;
use app\models\user\User;
use app\models\user\UserLevel;

class Up
{
    protected $order;
    protected $set;
    protected $user;

    public function __construct(Order $order)
    {
        $this->order = $order;
        $this->set = SetUserUp::getLastOne();
        $this->user = User::findOne(['id' => $this->order->user_id]);

        if ($this->user) {
            $this->upPrice();
            $this->upBox();
        }
    }

    // 一次性购买价格升级
    protected function upPrice()
    {
        $amount = $this->order->total_pay - $this->order->total_freight;

        if ($amount >= $this->set->svip1_amount) {
            // 经销商
            if ($this->user->level < UserLevel::$keys['svip1']) {
                $this->user->level = UserLevel::$keys['svip1'];
                $this->user->save();
            }
        } else if ($amount >= $this->set->vip3_amount) {
            // VIP
            if ($this->user->level < UserLevel::$keys['vip3']) {
                $this->user->level = UserLevel::$keys['vip3'];
                $this->user->save();
            }
        } else if ($amount >= $this->set->vip2_amount) {
            // 顾客
            if ($this->user->level < UserLevel::$keys['vip2']) {
                $this->user->level = UserLevel::$keys['vip2'];
                $this->user->save();
            }
        } else if ($amount >= $this->set->vip1_price) {
            // 会员[零售]
            if ($this->user->level < UserLevel::$keys['vip1']) {
                $this->user->level = UserLevel::$keys['vip1'];
                $this->user->save();
            }
        }
    }

    // 累计盒数升级
    private function upBox()
    {
        // 如果已经是区代了, 则不用计算了
        if ($this->user->level == UserLevel::$keys['svip2']) {
            return;
        }

        // 全部下单盒数
        $order_ids = Order::find()
            ->where(['user_id' => $this->user->id])
            ->andWhere(['is_extract' => 0])
            ->andWhere(['!=', 'status', Order::$statusMapping['NON_PAYMENT']])
            ->andWhere(['!=', 'status', Order::$statusMapping['CANCEL']])
            ->andWhere(['!=', 'status', Order::$statusMapping['DELETE']])
            ->andWhere(['!=', 'status', Order::$statusMapping['REFUND']])
            ->select(['id'])
            ->column();

        $items = OrderItem::find()
            ->where(['order_id' => $order_ids])
            ->with(['product'])
            ->asArray()
            ->all();

        $boxes = 0;

        foreach ($items as $item) {
            if ($item['product']) {
                $boxes += $item['quantity'] * $item['product']['store'];
            }
        }

        // var_dump($boxes);exit;

        if ($boxes >= $this->set->svip1_box) {
            // 经销商
            if ($this->user->level < UserLevel::$keys['svip1']) {
                $this->user->level = UserLevel::$keys['svip1'];
                $this->user->save();
            }
        } else if ($boxes >= $this->set->vip3_box) {
            // VIP
            if ($this->user->level < UserLevel::$keys['vip3']) {
                $this->user->level = UserLevel::$keys['vip3'];
                $this->user->save();
            }
        } else if ($boxes >= $this->set->vip2_box) {
            // 顾客
            if ($this->user->level < UserLevel::$keys['vip2']) {
                $this->user->level = UserLevel::$keys['vip2'];
                $this->user->save();
            }
        }
    }
}